package domain;

import util.JDBCUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class Product {
    private String id;
    private String name;
    private double price;
    private String category;
    private Integer pnum;
    private String imgurl;
    private String description;

    public List<Product> searchAll() throws SQLException {
        List<Product> productList = new ArrayList<Product>();
        Connection connection = JDBCUtil.getConnection();
        String sql = "SELECT * FROM products";
        Statement statement = connection.createStatement();
        ResultSet res = statement.executeQuery(sql);
        while (res.next()) {
            Product product = new Product();
            product.setId(res.getString(1));
            product.setName(res.getString(2));
            product.setPrice(res.getDouble(3));
            product.setCategory(res.getString(4));
            product.setPnum(res.getInt(5));
            product.setImgurl(res.getString(6));
            product.setDescription(res.getString(7));
            productList.add(product);
        }
        return productList;
    }
    public boolean add(Product product) throws SQLException {
        Connection connection = JDBCUtil.getConnection();
        String sql = "INSERT INTO `bookstore`.`products`(`id`, `name`, `price`, `category`, `pnum`, `imgurl`, `description`) VALUES (?, ?, ?, ?, ?, ?, ?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, String.valueOf(UUID.randomUUID()));
        preparedStatement.setString(2, product.getName());
        preparedStatement.setDouble(3, product.getPrice());
        preparedStatement.setString(4, product.getCategory());
        preparedStatement.setInt(5, product.getPnum());
        preparedStatement.setString(6, product.getImgurl());
        preparedStatement.setString(7, product.getDescription());
        int i = preparedStatement.executeUpdate();
        return i > 0;
    }


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public Integer getPnum() {
        return pnum;
    }

    public void setPnum(Integer pnum) {
        this.pnum = pnum;
    }

    public String getImgurl() {
        return imgurl;
    }

    public void setImgurl(String imgurl) {
        this.imgurl = imgurl;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}
